#include<Python.h>
#include"indeks.c"

/*
Ten plik opakowuje indeks.c tak, zeby mozna bylo z niego korzystac w pythonie
*/

static PyObject* put(PyObject* self, PyObject* args)
{
    int word,article,position;
 
    if (!PyArg_ParseTuple(args, "iii", &word,&article,&position))
        return NULL;
 
    return Py_BuildValue("i", _indeks_put(word,article,position));
}

static PyObject* mock_put(PyObject* self, PyObject* args)
{
    int word,article,position;
 
    if (!PyArg_ParseTuple(args, "iii", &word,&article,&position))
        return NULL;
 
    return Py_BuildValue("i", _mock_put(word,article,position));
}

static PyObject* start_structure(PyObject* self, PyObject* args)
{
 
    return Py_BuildValue("i", _start_structure() );
}

static PyObject* indeks_start(PyObject* self, PyObject* args)
{
    _indeks_start();
    return Py_BuildValue("i", 0) ;
}

static PyObject* indeks_serialize(PyObject* self, PyObject* args)
{
    char *fileName;
    if (!PyArg_ParseTuple(args, "s", &fileName ))
        return NULL;
    _indeks_serialize(fileName);
    return Py_BuildValue("i", 0) ;
}

static PyObject* indeks_deserialize(PyObject* self, PyObject* args)
{
    char *fileName;
    if (!PyArg_ParseTuple(args, "s", &fileName ))
        return NULL;
    _indeks_deserialize(fileName);
    return Py_BuildValue("i", 0) ;
}

static PyObject* fully_indeks_serialize(PyObject* self, PyObject* args)
{
    char *fileName;
    if (!PyArg_ParseTuple(args, "s", &fileName ))
        return NULL;
    _fully_indeks_serialize(fileName);
    return Py_BuildValue("i", 0) ;
}

static PyObject* fully_indeks_deserialize(PyObject* self, PyObject* args)
{
    char *fileName;
    if (!PyArg_ParseTuple(args, "s", &fileName ))
        return NULL;
    _fully_indeks_deserialize(fileName);
    return Py_BuildValue("i", 0) ;
}



static PyObject* getArticles(PyObject* self, PyObject* args)
{
    int word;
    if (!PyArg_ParseTuple(args, "i", &word))
        return NULL;
    
    PyObject* lista = PyList_New( vec_article[word].len);
    int i;
    for( i = 0 ; i < vec_article[word].len; i++)
    {
        PyList_SetItem(lista, (Py_ssize_t) i, PyLong_FromLong( vec_article[word].table[i]) );
    }
    return lista ;
}

static PyObject* getPositions(PyObject* self, PyObject* args)
{
    int word;
    if (!PyArg_ParseTuple(args, "i", &word))
        return NULL;
    
    PyObject* lista = PyList_New( vec_position[word].len);
    int i;
    for( i = 0 ; i < vec_position[word].len; i++)
    {
        PyList_SetItem(lista, (Py_ssize_t) i, PyLong_FromLong( (long) vec_position[word].table[i]) );
    }
    return lista ;
}

static PyObject* indexGet(PyObject* self, PyObject* args)
{
    int word;
    if (!PyArg_ParseTuple(args, "i", &word))
        return NULL;
    
    return Py_BuildValue ( "(iiii)", 
        vec_article[word].table,
        vec_article[word].len,
        vec_float[word].table,
        1
     );
}


 
static PyMethodDef HelloMethods[] =
{
     {"put", put, METH_VARARGS, "Greet somebody."},
     {"indeks_start", indeks_start, METH_VARARGS, "Greet somebody."},
     {"mock_put", mock_put, METH_VARARGS, "Greet somebody."},
     {"start_structure", start_structure, METH_VARARGS, "Greet somebody."},
     {"getArticles", getArticles, METH_VARARGS, "deprecated"},
     {"getPositions", getPositions, METH_VARARGS, "deprecated"},
     {"get", indexGet, METH_VARARGS, " " },
     {"indeks_serialize", indeks_serialize, METH_VARARGS, "Greet somebody."},
     {"indeks_deserialize", indeks_deserialize, METH_VARARGS, "Greet somebody."},
     {"fully_serialize", fully_indeks_serialize, METH_VARARGS, "Greet somebody."},
     {"fully_deserialize", fully_indeks_deserialize, METH_VARARGS, "Greet somebody."},
     {NULL, NULL, 0, NULL}
};
 
PyMODINIT_FUNC initindeks(void)
{
     (void) Py_InitModule("indeks", HelloMethods);
}
